package com.yl.project.lvyou.frontapp.controller;

import com.yl.common.utils.Md5Utils;
import com.yl.common.utils.StringUtils;
import com.yl.common.utils.file.FileUploadUtils;
import com.yl.framework.config.YlConfig;
import com.yl.framework.web.controller.BaseController;
import com.yl.framework.web.domain.AjaxResult;
import com.yl.project.lvyou.comments.domain.ScenicComments;
import com.yl.project.lvyou.comments.service.IScenicCommentsService;
import com.yl.project.lvyou.likes.domain.ScenicCommentLikes;
import com.yl.project.lvyou.likes.service.IScenicCommentLikesService;
import com.yl.project.lvyou.replies.domain.ScenicCommentReplies;
import com.yl.project.lvyou.replies.service.IScenicCommentRepliesService;
import com.yl.project.lvyou.user.domain.ScenicUser;
import com.yl.project.lvyou.user.service.IScenicUserService;
import com.yl.project.system.dict.domain.DictData;
import com.yl.project.system.dict.service.IDictDataService;
import com.yl.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;


@CrossOrigin
@Controller
@RequestMapping("/lvyou/user")
public class FrontUserController extends BaseController {

    @Autowired
    private IScenicUserService scenicUserService;

    @Autowired
    private IDictDataService dictDataService;

    @Autowired
    private IScenicCommentsService scenicCommentsService;

    @Autowired
    private IScenicCommentRepliesService scenicCommentRepliesService;

    @Autowired
    private IScenicCommentLikesService scenicCommentLikesService;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @CrossOrigin(origins = "*")
    @PostMapping("/userLogin")
    @ResponseBody
    public AjaxResult userLogin(@RequestBody ScenicUser user)
    {


        if(StringUtils.isNotEmpty(user.getUsername())&&StringUtils.isNotEmpty(user.getPassword())){
            ScenicUser scenicUser =scenicUserService.selectOneByUserName(user.getUsername());
            if(scenicUser!=null){
                String password=Md5Utils.hash(user.getPassword());
                if(scenicUser.getPassword().equals(password)){
                    List<DictData> Lvyoupianhao=dictDataService.selectUserLvyoupianhao(scenicUser.getId());
                    scenicUser.setDictData(Lvyoupianhao);
                    return AjaxResult.success(scenicUser);
                }else{
                    return AjaxResult.error("密码不正确");
                }

            }else{
                return AjaxResult.error("用户名不存在，请注册!");
            }

        }else{
            return AjaxResult.error("请输入用户名和密码!!!");
        }
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @CrossOrigin(origins = "*")
    @PostMapping("/userRegister")
    @ResponseBody
    public AjaxResult userRegister(@RequestBody ScenicUser user)
    {

        if(StringUtils.isNotEmpty(user.getUsername())&&StringUtils.isNotEmpty(user.getPassword())&&StringUtils.isNotEmpty(user.getMobile())){
            ScenicUser scenicUser =scenicUserService.selectOneByUserName(user.getUsername());
            if(scenicUser==null){
                user.setPassword(Md5Utils.hash(user.getPassword()));
                scenicUserService.insertScenicUser(user);
                return AjaxResult.success("注册成功！");
            }else{
                return AjaxResult.error("用户名已存在，请注册!");
            }
        }else{
            return AjaxResult.error("请输入用户名密码和手机号!");
        }
    }
/**
     * 用户修改密码
     * @param
     * @return
     */
    @CrossOrigin(origins = "*")
    @GetMapping("/updateUserPassWord")
    @ResponseBody
    public AjaxResult updateUserPassWord( Long id,String oldPassword ,String newPassword)
    {
        ScenicUser scenicUser =scenicUserService.selectScenicUserById(id);
        if(scenicUser!=null){
            String password=Md5Utils.hash(oldPassword);
            if(password.equals(scenicUser.getPassword())){
                scenicUser.setPassword(Md5Utils.hash(newPassword));
                scenicUserService.updateScenicUser(scenicUser);
            }else{
                return AjaxResult.error("旧密码不正确");
            }

            return AjaxResult.success("修改成功！");
        }else{
            return AjaxResult.error("修改失败");
        }

    }


    /**
     * 上传头像
     * @param file
     * @return
     */
    @CrossOrigin(origins = "*")
    @PostMapping("/avatar")
    @ResponseBody
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file,@RequestParam Long id,@RequestParam String username)
    {

        try
        {
            if (!file.isEmpty())
            {
                ScenicUser user=new ScenicUser();
                user.setId(id);
                user.setUsername(username);
                String avatar = FileUploadUtils.upload(YlConfig.getAvatarPath(), file);
                user.setAvatar(avatar);
                scenicUserService.updateScenicUser(user);
                return AjaxResult.success(user);
            }
            return AjaxResult.error("文件不存在!!");
        }
        catch (Exception e)
        {
            return AjaxResult.error("系统异常！！！");
        }
    }

    /**
     * 保存个人信息
     * @param user
     * @return
     */
    @CrossOrigin(origins = "*")
    @PostMapping("/updateUserInfo")
    @ResponseBody
    public AjaxResult updateUserInfo(@RequestBody ScenicUser user)
    {
            scenicUserService.updateScenicUser(user);
            return AjaxResult.success();
    }

    /**
     * 景区评论列表
     *
     * @return
     */
    @CrossOrigin(origins = "*")
    @GetMapping("/comments")
    @ResponseBody
    public AjaxResult comments(ScenicComments scenicComments)
    {
       startPage();
      List<ScenicComments> list = scenicCommentsService.selectScenicCommentsListApp(scenicComments);
       return  AjaxResult.success(getDataTable(list));
    }

    /**
     * 发表评论
     * @param scenicComments
     * @return
     */
    @CrossOrigin(origins = "*")
    @PostMapping("/commentsAdd")
    @ResponseBody
    public AjaxResult commentsAdd(@RequestBody ScenicComments scenicComments)
    {
        return  AjaxResult.success(scenicCommentsService.insertScenicComments(scenicComments));
    }

    /**
     * 回复评论保存
     * @param scenicCommentReplies
     * @return
     */
    @CrossOrigin(origins = "*")
    @PostMapping("/comment/reply")
    @ResponseBody
    public AjaxResult reply(@RequestBody ScenicCommentReplies scenicCommentReplies)
    {
        ScenicComments scenicComments = new ScenicComments();
        scenicComments.setId(scenicCommentReplies.getCommentId());
        scenicComments.setReplyCount(scenicCommentReplies.getReplyCount()+1);
        scenicCommentsService.updateScenicComments(scenicComments);
        return  AjaxResult.success(scenicCommentRepliesService.insertScenicCommentReplies(scenicCommentReplies));
    }

    /**
     * 景区回复列表
     *
     * @return
     */
    @CrossOrigin(origins = "*")
    @GetMapping("/comment/replies")
    @ResponseBody
    public AjaxResult replies(ScenicCommentReplies scenicCommentReplies)
    {
        startPage();
        List<ScenicCommentReplies> list = scenicCommentRepliesService.selectScenicCommentRepliesListApp(scenicCommentReplies);
        return  AjaxResult.success(getDataTable(list));
    }

    /**
     * 景区评论点赞
     *
     * @return
     */
    @CrossOrigin(origins = "*")
    @GetMapping("/comment/like")
    @ResponseBody
    public AjaxResult like(ScenicCommentLikes scenicCommentLikes)
    {
        //判断isLiked 是否为0 如果是0 则需要新增  否则进行删除
        if("0".equals(scenicCommentLikes.getIsLiked())){
            scenicCommentLikesService.insertScenicCommentLikes(scenicCommentLikes);
            ScenicComments scenicComments = new ScenicComments();
            scenicComments.setId(scenicCommentLikes.getCommentId());
            scenicComments.setLikeCount(scenicCommentLikes.getLikeCount()+1);
            scenicCommentsService.updateScenicComments(scenicComments);
            scenicCommentLikes.setLikeCount(scenicComments.getLikeCount());
        }else{
            ScenicComments scenicComments = new ScenicComments();
            scenicComments.setId(scenicCommentLikes.getCommentId());
            scenicComments.setLikeCount(scenicCommentLikes.getLikeCount()-1);
            scenicCommentsService.updateScenicComments(scenicComments);
            scenicCommentLikes.setLikeCount(scenicComments.getLikeCount());
            scenicCommentLikesService.deleteLike(scenicCommentLikes);
        }
        return  AjaxResult.success(scenicCommentLikes);
    }

}
